package com.sgb.zhaocai.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.sgb.zhaocai.base.BaseApi;
import com.sgb.zhaocai.entity.ProjectListEntity;
import com.sgb.zhaocai.entity.RequestBean;
import com.sgb.zhaocai.view.demand.fragment.ProjectFragment;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.view.areapopup.AddressEntity;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2021/7/1
 * AUTHOR:F_Q
 * Description:工程项目
 */
public class ProjectModel extends ViewModel {
    //项目列表
    public MutableLiveData<List<ProjectListEntity>> projectlist = new MutableLiveData<>();
    //排序集合
    public MutableLiveData<List<CommonPopEntity>> SortTypeList = new MutableLiveData<>();
    //工程类型
    public MutableLiveData<List<CommonPopEntity>> ProjectTypeList = new MutableLiveData<>();
    //时间
    public MutableLiveData<List<CommonPopEntity>> TimeList = new MutableLiveData<>();
    //地址列表
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> mAddressData = new MutableLiveData<>();
    public boolean isLastPage;//是否可以加载更多

    /**
     * 工程项目H5详情处理
     *
     * @param id
     * @param longitudes
     * @param latitudes
     */
    public void getH5Url(ProjectFragment fragment,String id, String longitudes, String latitudes) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getUrlSetting("engineeringProject").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    String webUrl = data.getData().getUrl() + "?id=" + id + "&lng=" + longitudes + "&lat=" + latitudes;
                    BaseApi.startWeb(fragment.getContext(), webUrl);
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    /**
     * 列表请求
     */
    public void ProjectList(ProjectFragment fragment, RequestBean bean) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getProjectList(bean).enqueue(new ICallBack<BaseEntity<ListResponse<List<ProjectListEntity>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<ProjectListEntity>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    isLastPage = data.getData().getCurrentPage() >= data.getData().getTotal();
                    projectlist.setValue(data.getData().getList());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }

    /**
     * 地址详情
     */
    public void getAddressModel(ProjectFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    mAddressData.setValue(data.getData());
                }
            }
        });
    }



    //工程类型
    public void getProjectTypeList(ProjectFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getFindEngineeringTypeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    ProjectTypeList.setValue(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
    //排序
    public void getSortType() {
        List<CommonPopEntity> datas = new ArrayList<>();
        datas.add(new CommonPopEntity("1", "综合排序"));
        datas.add(new CommonPopEntity("3", "创建时间↓"));
        datas.add(new CommonPopEntity("2", "创建时间↑"));
        datas.add(new CommonPopEntity("4", "距离↓"));
        datas.add(new CommonPopEntity("6", "项目造价↓"));
        datas.add(new CommonPopEntity("5", "项目造价↑"));
        SortTypeList.setValue(datas);
    }

    //创建时间
    public void getTime(ProjectFragment fragment) {
        DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        BaseApi.getApi().getfindProjectTimeSelect().enqueue(new ICallBack<BaseEntity<List<CommonPopEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CommonPopEntity>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(BaseApi.RESPONSE_OK)){
                    TimeList.setValue(data.getData());
                }else {
                    MToast.showToast(fragment.getContext(),data.getMsg());
                }
            }
        });
    }
}
